package com.SmartCare.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author 缘友一世
 * date 2023/7/16-12:52
 */
@Configuration
@Slf4j
public class RedisConfig {
    /**
     默认的key序列化器JdkSerializationRedisSerializer，导致Redis中后的数据和原始数据有差别，
     故设置为StringRedisSerializer序列化器。保证redis和原始数据的一致，使用StringRedisSerializer序列化器
     */
   @Bean
   public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
       log.info("开始创建redis模板对象");
       RedisTemplate<String, String> redisTemplate = new RedisTemplate();
       //设置redis的连接工厂对象
       redisTemplate.setConnectionFactory(redisConnectionFactory);
       //设置redis key的序列化器
       redisTemplate.setKeySerializer(new StringRedisSerializer());
       //设置redis value的序列化器
       redisTemplate.setValueSerializer(new StringRedisSerializer());

       return redisTemplate;
   }
}
